package com.changgou.oauth.controller;

import com.changgou.oauth.service.LonginUserService;
import com.changgou.oauth.util.AuthToken;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping(value = "/user")
public class UserLonginController {

    @Autowired
    private LonginUserService longinUserService;

    //获取 客户端Id
    @Value("${auth.clientId}")
    private String clientId;

    //客户端密钥
    @Value("${auth.clientSecret}")
    private String clientSecret;

    @RequestMapping(value = "/login")
    public Result login(String username,String password) throws UnsupportedEncodingException {
        String grant_type ="password";
        AuthToken authToken = longinUserService.longin(username, password, clientId, clientSecret, grant_type);

        if (authToken!=null){
            return new Result(true, StatusCode.OK,"登陆成功",authToken);
        }


        return new Result(false,StatusCode.LOGINERROR,"登录失败");
    }
}
